// 认证相关工具函数

const TOKEN_KEY = 'smartui_token'
const USER_INFO_KEY = 'smartui_user_info'

// 登录函数
export function login(username, password) {
  return new Promise((resolve) => {
    // 模拟登录请求
    setTimeout(() => {
      if (username && password) {
        const userInfo = {
          id: Date.now(),
          username: username,
          avatar: '/static/avatar.png',
          description: 'SmartUI用户',
          followCount: 12,
          fansCount: 34,
          likeCount: 56
        }
        
        // 保存token和用户信息
        uni.setStorageSync(TOKEN_KEY, 'mock_token_' + Date.now())
        uni.setStorageSync(USER_INFO_KEY, userInfo)
        
        resolve({
          success: true,
          data: userInfo
        })
      } else {
        resolve({
          success: false,
          message: '用户名或密码错误'
        })
      }
    }, 1000)
  })
}

// 退出登录
export function logout() {
  uni.removeStorageSync(TOKEN_KEY)
  uni.removeStorageSync(USER_INFO_KEY)
}

// 检查是否登录
export function isLogin() {
  return !!uni.getStorageSync(TOKEN_KEY)
}

// 获取用户信息
export function getUserInfo() {
  return uni.getStorageSync(USER_INFO_KEY)
}

// 获取token
export function getToken() {
  return uni.getStorageSync(TOKEN_KEY)
}